public class demo1 {

    public static int BF(String str, String sub, int pos){
        if(str == null || sub == null) return -1;
        int lenStr = str.length();
        int lenSub = sub.length();
        if(lenSub == 0 || lenStr == 0) return -1;
        if(pos >= lenStr || pos < 0) return -1;

        int i = pos;   //遍历主字符串
        int j = 0;     //遍历子字符串
        while(i < lenStr && j < lenSub){
            if(str.charAt(i) == sub.charAt(j)){
                i++;
                j++;
            }else{
                i = i - j + 1;
                j = 0;
            }
        }
        if(j >= lenSub){
            return i - j;
        }
        return -1;
    }

    public static void main(String[] args) {
        String str = "abcabcda";
        String sub = "bcad";
        System.out.println(BF(str,sub,0));
        System.out.println(str.indexOf(sub));

    }
}
